import api from '../../http/modules/api.js';

export default {
    namespaced: true,
    state: {
        pages: 0,
        total: 0,
        subjectsSearchData: {
            type: 'name',
            value: '',
            pageSize: 4,
            currentPage: 1,
        },
        subjectsData: [],
    },
    mutations: {
        //获取专业数据成功后，更新专业数据
        setSubjectsData(state, newData) {
            state.pages = newData.pages;
            state.total = newData.total;
            state.subjectsData = newData.result;
        },
        // 翻页更改数据
        setCurrentPage(state, newData) {
            state.subjectsSearchData.currentPage = newData;
        },
        // 更改每页条数
        setPageSize(state, newData) {
            state.subjectsSearchData.pageSize = newData;
        }
    },
    actions: {
        //获取专业列表数据
        async getSubjects(context) {
            const res = await api.subjects.getSubjects(context.state.subjectsSearchData);
            if (res.code) {
                context.commit('setSubjectsData', res.data);
            }
        },
        // 根据Id删除一个专业
        async deleteOneSubject(context, _id) {
            const res = await api.subjects.deleteOneSubject({
                _id
            });
            return res;
        },
        // 通过Id修改一个专业
        async updateOneSubject(context, updateData) {
            const res = await api.subjects.updateOneSubject(updateData)
            return res;
        },
        // 新增一个专业
        async addOneSubjects(context,addSubjectsData){
            const res = await api.subjects.addOneSubjects(addSubjectsData)
            return res
        }
    },
};